<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/7
 * Time: 14:23
 */
include_once(ROOTDIR.'include/ap.class.php');
include_once(ROOTDIR.'include/mssg.class.php');

class Gateway_con extends Ap
{
    //网关验证
    public function index()
    {
        if (get_magic_quotes_gpc()) {
            foreach ( $_POST as $key => $value ) {
                $_POST [$key] = stripslashes ( $value );
            }
            foreach ( $_GET as $key => $value ) {
                $_GET [$key] = stripslashes ( $value );
            }
            foreach ( $_REQUEST as $key => $value ) {
                $_REQUEST [$key] = stripslashes ( $value );
            }
        }

        $userid = $this->getRequest ( "userid" );
        $sign = $this->getRequest ( "sign" );
        $sign_type = $this->getRequest ( "sign_type" );
        $biz_content = $this->getRequest ( "biz_content" );
        $service = $this->getRequest ( "service" );
        $charset = $this->getRequest ( "charset" );

        if (empty ( $sign ) || empty ( $sign_type ) || empty ( $biz_content ) || empty ( $service ) || empty ( $charset )) {
            echo "some parameter is empty.";
            $this->writeLog ( "some parameter is empty.");
            exit ();
        }

        // 收到请求，先验证签名
        //$sign_verify = $this->rsaCheckV2 ( $data, $this->alipay_public_key );
        $sign_verify = $this->rsaCheckV2 ( $_REQUEST, $this->alipay_public_key );
        /*$data = array(
            'content'  => $sign_verify,
            'gcontent' => '',
            'tim'      => date('Y-m-d H:i:s', time()),
        );
        $this->setdata('errortest', $data);*/

        if (! $sign_verify) {
            // 如果验证网关时，请求参数签名失败，则按照标准格式返回，方便在服务窗后台查看。
            if ($this->getRequest ( "service" ) == "alipay.service.check") {

                $this->verifygw ( false, $userid);
            } else {
                echo "sign verfiy fail.";
                //$this->writeLog ( "sign verfiy fail.");
            }
            exit ();
        }

        // 验证网关请求
        if ($this->getRequest ( "service" ) == "alipay.service.check") {

            $this->verifygw (true, $userid );
        }
        else if ($this->getRequest ( "service" ) == "alipay.mobile.public.message.notify") {
            // 处理收到的消息
            $msg = new Msg();
            $msg->Message($biz_content, $userid);
        }
    }

    //验证网关方法
    public function verifygw($is_sign_success, $userid) {
        $biz_content = stripslashes($this->getRequest ( "biz_content" ));
        $xml = simplexml_load_string ($biz_content, 'SimpleXMLElement', LIBXML_NOCDATA);
        $EventType = (string)$xml->EventType;

        $user = $this->getapinfo($userid);

        if ($EventType == "verifygw") {
            if ($is_sign_success) {
                $response_xml = "<success>true</success><biz_content>" . $this->getPublicKeyStr($user['merchant_public_key'])."</biz_content>";

            } else { // echo $response_xml;
                $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $this->getPublicKeyStr($user['merchant_public_key']). "</biz_content>";
            }

            $return_xml = $this->sign_response ( $response_xml, $this->charset, $user['merchant_private_key']);
            //$this->writeLog ( "response_xml: " . $return_xml );

            echo $return_xml;
            exit ();
        }
    }


}